﻿@{
    ViewBag.Title = "Index_App";
    Layout = "~/Views/Shared/_Layout_App.cshtml";
    if (!YJ.Platform.WeiXin.Organize.CheckLogin())
    {
        Response.End();
        return;
    }
}

<form id="form1">
    @{
        string extName = Request.QueryString["filetype"];
        string filesWebSite = Url.Content("~/Controls/UploadFiles/");
        Page.CheckApp = false;
        YJ.Platform.Files BFiles = new YJ.Platform.Files();
        string rootPath = BFiles.GetRootPath();
        string query = "&eid=" + Request.QueryString["eid"] + "&files=" + Request.QueryString["files"] + "&filetype=" + Request.QueryString["filetype"] + "&iframeid=" + Request.QueryString["iframeid"];
        }
        <div style="height:8px;"></div>
            <link href="~/Scripts/WebUploader/webuploader.css" rel="stylesheet" />
            <script src="~/Scripts/WebUploader/webuploader.min.js"></script>
            <table cellpadding="0" cellspacing="1" border="0" width="98%" align="center" style="margin-top:8px;">
                <tr>
                    <td style="height:40px;" id="uploadtable"><div id="picker" style="float:left;">选择文件</div>
                        <div class="uploadpress" style="width:60px;">
                            <div id="uploadpressdiv"></div>
                        </div>
                    </td>
                    <td align="right" style="padding-right:20px;">
                        <input type="button" class="mybutton" value="&nbsp;确&nbsp;&nbsp;认&nbsp;" onclick="confirm1();" />
                        <input type="button" class="mybutton" value="&nbsp;关&nbsp;&nbsp;闭&nbsp;" onclick="closewin();" />
                    </td>
                </tr>
            </table>
            <div id="queue" style="margin:0 auto 5px 0;"></div>
            <div id="filelist">
                <table cellpadding="0" cellspacing="1" border="0" id="filetable" width="98%" class="listtable" style="width:98%; margin:0 auto;">
                    <thead>
                        <tr>
                            <th style="width:60%">文件</th>
                            <th style="width:20%;">大小</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                    @{
                        string files = Request.QueryString["files"];
                        if (!files.IsNullOrEmpty())
                        {
                            string[] filesArray = files.Split('|');
                            foreach (string file in filesArray)
                            {
                                string file1 = file.DesDecrypt();
                                System.IO.FileInfo fi = new System.IO.FileInfo(System.IO.Path.Combine(rootPath, file1));
                                if (!fi.Exists)
                                {
                                    continue;
                                }
                                string id = fi.FullName.DesEncrypt();
                                int type = file1.StartsWith("UploadFiles", StringComparison.CurrentCultureIgnoreCase) ? 0 : 1;
                     
                        <tr id="@id">
                            <td style="background:#ffffff;"><a target="_blank" href="~/Content/Show.ashx?id=@id" style="background:url(../../Images/ico/doc_stand.png) no-repeat; padding-left:22px;">@fi.Name</a></td>
                            <td style="background:#ffffff;text-align:right; padding-right:3px;">@(decimal.Round((fi.Length/1024),0))KB</td>
                            <td style="background:#ffffff;"><input type="hidden" name="delfile" value="@file" /><a class="deletelink" href="javascript:void(0);" onclick="delFile('@id',@type);">删除</a></td>
                        </tr>
                    
                            }
                        }    
                        }
                    </tbody>
                </table>
            </div>
   </form>
	        <script type="text/javascript">
	            var filesWebSite = '@filesWebSite';
	            var win = new RoadUI.Window();
	            var eid = '@Request.QueryString["eid"]';
	            var uploadDir = "@BFiles.GetUploadPath().DesEncrypt()";
	            $(function ()
	            {
	                var uploader = WebUploader.create({
	                    // swf文件路径
	                    swf: '../Scripts/WebUploader/Uploader.swf',
	                    // 文件接收服务端。
	                    server: '../../Files/FileUpload',
	                    // 选择文件的按钮。可选。
	                    // 内部根据当前运行是创建，可能是input元素，也可能是flash.
	                    pick: '#picker',
	                    // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
	                    resize: false,
	                    //选择文件后自动上传
	                    auto: true,
	                    //分片上传大文件
	                    //chunked: true,
	                    // 只允许选择图片文件。
	                    //accept: {
	                    //    title: 'Images',
	                    //    extensions: 'gif,jpg,jpeg,bmp,png',
	                    //    mimeTypes: 'image/*'
	                    //},
	                    //其他参数
	                    formData: { "dir": uploadDir, "userid": "@YJ.Platform.Users.CurrentUserID" }
            });
            // 当有文件被添加进队列的时候
            uploader.on('fileQueued', function (file)
            {
                $(".uploadpress").show();
            });

            // 文件上传过程中创建进度条实时显示。
            uploader.on('uploadProgress', function (file, percentage)
            {
                var width = Math.round(percentage * 100);
                $("#uploadpressdiv").text(width + '%').css("width", Math.round(280 / (percentage < 1 ? percentage * 100 : percentage)).toString() + "px");
            });

            uploader.on('uploadSuccess', function (file, response)
            {
                if (response.error)
                {
                    alert(response.error);
                }
                else
                {
                    addFile(file.name, response.id, response.id1, response.size, 0);
                }

            });
            //所有文件上传完成发生
            uploader.on('uploadFinished', function ()
            {
                $(".uploadpress").hide();
            });
        });
        // id 完整路径 id1 不包含根路径 type(0上传的文件 1我的文件中选择的文件)
        function addFile(name, id, id1, size, type)
        {
            if ($("#" + id).size() > 0)
            {
                return false;
            }
            var tr = '<tr id="' + id + '">';
            tr += '<td style="background:#ffffff;">';
            tr += '<a href="../../Content/Show.ashx?id=' + id + '" target="_blank" style="background:url(../../Images/ico/doc_stand.png) no-repeat; padding-left:22px;">';
            tr += name
            tr += '</a>';
            tr += '</td>';
            tr += '<td style="background:#ffffff;text-align:right; padding-right:3px;">';
            tr += size
            tr += '</td>';
            tr += '<td style="background:#ffffff;">';
            tr += '<input type="hidden" value="' + id1 + '" name="delfile"/><a class="deletelink" href="javascript:void(0);" onclick="delFile(\'' + id + '\',' + type + ')">删除</a>';
            tr += '</td>';
            tr += '</tr>';
            $("#filetable tbody").append(tr);
            return true;
        }
        function delFile(id)
        {
            $("#" + id).remove();
        }
        function checkallbox(box)
        {
            $("input[name='delfile']").prop("checked", $(box).prop("checked"));
        }
        function confirm1()
        {
            var title = [];
            var value = [];
            $("#filetable tbody tr").each(function ()
            {
                var filename = $("td:eq(0)", $(this)).text();
                var filepathname = $("input[name='delfile']", $(this)).val();
                title.push(filename);
                value.push(filepathname);
            });
            var ele = win.getOpenerElement(eid);
            var ele1 = win.getOpenerElement(eid + "_text");
            if (ele1 != null && ele1.size() > 0)
            {
                if (value.length == 0)
                {
                    ele1.val('');
                }
                else
                {
                    ele1.val('共' + value.length + '个文件');
                }
            }
            if (ele != null && ele.size() > 0)
            {
                ele.val(value.join('|'));
            }
            else
            {
                ele.val('');
            }
            closewin();
        }
        function closewin()
        {
            try
            {
                $('#file_upload').uploadify('destroy');
            } catch (e)
            {

            }
            win.close();
        }
        function delselect()
        {
            if ($(":checkbox[name='delfile']:checked").size() == 0)
            {
                alert("您没有选择要删除的文件!"); return;
            }
            $(":checkbox[name='delfile']:checked").each(function ()
            {
                var box = $(this);
                var file = box.val();
                $.getScript(filesWebSite + "Delete?file=" + file, function ()
                {
                    if ("1" == json.success)
                    {
                        box.parent().parent().remove();
                    }
                    else
                    {
                        alert(json.message);
                    }
                });
            });

        }
	</script>